package com.springboot.others.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;

/*
 *DATE:2025/7/22
 */
@Controller
@Slf4j
public class UploadController {

    @Value("${file.upload.path}")
    private String Path;

    @GetMapping("/")
    public String uploadPage() {
        log.info("Path:{}", Path);
        return "upload";
    }
    @PostMapping("/upload")
    @ResponseBody
    public String upload(@RequestPart("file") MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        String filePath = Path + File.separator + fileName;
        File dest = new File(filePath);
        file.transferTo( dest);
        log.info("上传成功，文件名：{}", fileName);
        return "Upload file success : " + dest.getAbsolutePath();
    }
    @PostMapping("/uploads")
    @ResponseBody
    public String create(@RequestPart MultipartFile[] files) throws IOException {
        StringBuffer message = new StringBuffer();

        for (MultipartFile file : files) {
            String fileName = file.getOriginalFilename();
            String filePath = Path + File.separator + fileName;

            File dest = new File(filePath);
            Files.copy(file.getInputStream(), dest.toPath());
            message.append("Upload file success : " + dest.getAbsolutePath()).append("<br>");
        }
        return message.toString();
    }
    @RequestMapping("/download")
    public void download(String name, HttpServletResponse response) throws IOException {
        System.out.println("name:"+name);
        //文件的完整路径
        String real_path = Path + File.separator +name;

        //设置响应头  告知浏览器，要以附件的形式保存内容   filename=浏览器显示的下载文件名
        response.setHeader("content-disposition","attachment;filename="+name);

        //读取目标文件，写出给客户端
        IOUtils.copy(new FileInputStream(real_path), response.getOutputStream());

    }
}
